package com.sskj.mine.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerHorizenLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.AccountEnum;
import com.sskj.mine.bean.AccountTitleBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.MineFragmentPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


@Route(path = RConfig.MINE_FRAGMENT_MAIN)
public class MineFragment extends BaseFragment<MineFragmentPresenter> {
    @BindView(R2.id.ivHead)
    ImageView ivHead;
    @BindView(R2.id.tvAccount)
    TextView tvAccount;
    @BindView(R2.id.ivSetting)
    ImageView ivSetting;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.stvGRZL)
    SuperTextView stvGRZL;
    @BindView(R2.id.stvZHAQ)
    SuperTextView stvZHAQ;
    @BindView(R2.id.stvCJPT)
    SuperTextView stvCJPT;
    @BindView(R2.id.stvFLQ)
    SuperTextView stvFLQ;
    @BindView(R2.id.stvKTQ)
    SuperTextView stvKTQ;
    @BindView(R2.id.stvYQFY)
    SuperTextView stvYQFY;
    @BindView(R2.id.stvTBDZ)
    SuperTextView stvTBDZ;
    @BindView(R2.id.stvSQSJ)
    SuperTextView stvSQSJ;
    @BindView(R2.id.stvSKFS)
    SuperTextView stvSKFS;
    @BindView(R2.id.stvGGZX)
    SuperTextView stvGGZX;
    @BindView(R2.id.stvLLWM)
    SuperTextView stvLLWM;
    @BindView(R2.id.btExit)
    Button btExit;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter<AccountTitleBean> slimAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment_mine;
    }

    @Override
    public MineFragmentPresenter getPresenter() {
        return new MineFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && userViewModel != null) {
            userViewModel.update();
        }
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        recyclerView.addItemDecoration(new DividerHorizenLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        HashMap<AccountEnum, Integer> bgMap = new HashMap<>();
        bgMap.put(AccountEnum.钱包账户, R.mipmap.mine_icon_wallet_bg);
        bgMap.put(AccountEnum.币币账户, R.mipmap.mine_icon_bibi_bg);
        bgMap.put(AccountEnum.法币账户, R.mipmap.mine_icon_fabi_account_ng);
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_account_mine, new SlimInjector<AccountTitleBean>() {
            @Override
            public void onInject(AccountTitleBean data, IViewInjector injector, List payloads) {

                injector
                        .background(R.id.clRoot, bgMap.get(data.getAccountEnum()))
                        .text(R.id.tvTitle, data.getAccountEnum().getTitle())
                        .text(R.id.tvUsdt, data.getUsdt())
                        .text(R.id.tvCny, String.format("≈ %s CNY", data.getCny()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ArrayList<AccountTitleBean> accountTitleBeans = new ArrayList<>();
        accountTitleBeans.add(new AccountTitleBean(AccountEnum.钱包账户));
        accountTitleBeans.add(new AccountTitleBean(AccountEnum.币币账户));
        accountTitleBeans.add(new AccountTitleBean(AccountEnum.法币账户));
        slimAdapter.updateData(accountTitleBeans);
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                        return;
                    }
                    ARouter.getInstance().build(RConfig.MINE_ACCOUNT).withSerializable(Constans.ACCOUNT_ENUM, slimAdapter.getDataItem(i).getAccountEnum()).navigation();
                });
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                userViewModel.update();
            }
        });
    }

    @Override
    protected void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                slimAdapter.getDataItem(0).setUsdt(userData.getQianbao().getGuzhi_usdt());
                slimAdapter.getDataItem(0).setCny(userData.getQianbao().getGuzhi_cny());
                slimAdapter.getDataItem(1).setUsdt(userData.getBibi().getGuzhi_usdt());
                slimAdapter.getDataItem(1).setCny(userData.getBibi().getGuzhi_cny());
                slimAdapter.getDataItem(2).setUsdt(userData.getFabi().getGuzhi_usdt());
                slimAdapter.getDataItem(2).setCny(userData.getFabi().getGuzhi_cny());

                slimAdapter.notifyDataSetChanged();
                if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
                    coolRefreshView.setRefreshing(false);
                }
            } else {
                userData = null;
            }
            updateUI();
        });
    }

    private void updateUI() {
        if (userData == null) {
            tvAccount.setText(App.INSTANCE.getString(R.string.mine_mineFragment1));
            ivHead.setImageResource(R.mipmap.mine_user_head);
            ClickUtil.click(tvAccount, () -> {
                LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
                logoutProvider.logout();
            });
            btExit.setVisibility(View.GONE);
        } else {
            if (TextUtils.isEmpty(userData.getAvatar())) {
                ivHead.setImageResource(R.mipmap.mine_user_head);
            } else {
                ImageUtil.setCircleImage(HttpConfig.BASE_IMG_URL + userData.getAvatar(), ivHead);
            }
            tvAccount.setText(HideUtil.getPhoneHide(userData.getPhone()));
            ClickUtil.click(tvAccount, () -> {
            });
            if (userData.getIs_shop() == 0) {
                stvSQSJ.setRightString("");
            } else if (userData.getIs_shop() == 2) {
                stvSQSJ.setRightString("");
            } else if (userData.getIs_shop() == 3) {
                stvSQSJ.setRightString("");
            } else if (userData.getIs_shop() == 1) {
                stvSQSJ.setRightString(App.INSTANCE.getString(R.string.mine_mineFragment3));
            }
            btExit.setVisibility(View.VISIBLE);

        }
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(ivSetting, () -> {
            ARouter.getInstance().build(RConfig.MINE_SETTING).navigation();
        });
        ClickUtil.click(stvGRZL, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_PERSON).navigation();
        });
        ClickUtil.click(stvZHAQ, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SECURITY_CENTER).navigation();
        });
        ClickUtil.click(stvCJPT, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.SUPER_PLATFORM).navigation();
        });
        ClickUtil.click(stvFLQ, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.SUPER_WELFARE_DISTRICT).navigation();
        });
        ClickUtil.click(stvKTQ, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.SUPER_SUPER_AIR_DROP).navigation();
        });
        ClickUtil.click(stvYQFY, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_INVITE).navigation();

        });
        ClickUtil.click(stvTBDZ, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.TIBI_ADDRESS_LIST).navigation();

        });
        ClickUtil.click(stvSQSJ, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkAuth(getActivity(), userData)) {
                return;
            }
            if (userData.getIs_shop() == 0) {

                ARouter.getInstance().build(RConfig.MINE_SELLER_APPLY).navigation();
            } else if (userData.getIs_shop() == 2) {

                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment4));
            } else if (userData.getIs_shop() == 3) {

                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment5));
            } else if (userData.getIs_shop() == 1) {
                TipUtil.getSureCancelTip(getActivity(), App.INSTANCE.getString(R.string.mine_mineFragment6), App.INSTANCE.getString(R.string.mine_mineFragment7), App.INSTANCE.getString(R.string.mine_mineFragment8), () -> {
                    mPresenter.unBindShop();
                });
            }

        });
        ClickUtil.click(stvSKFS, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.PAY_PAY_TYPE).navigation();

        });
        ClickUtil.click(stvLLWM, () -> {
            ARouter.getInstance().build(RConfig.MINE_CONTRACT_US).navigation();

        });
        ClickUtil.click(stvGGZX, () -> {
            ARouter.getInstance().build(RConfig.APP_GONGGAO_LIST).navigation();

        });
        ClickUtil.click(btExit, () -> {
            TipUtil.getSureCancelTip(getActivity(), App.INSTANCE.getString(R.string.mine_mineFragment9), () -> {

                LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
                logoutProvider.logout();
            });
        });
    }

    public void unBindShopSuccess() {
        userViewModel.update();
    }
}
